package cqrtplm.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.ty.basic.entity.org.UserDO;
import com.ty.basic.exception.MessageException;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RTErrorCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cqrtplm.mapper.RtCertBasecertMapper;
import cqrtplm.entity.RtCertBaseCertDO;
import cqrtplm.service.RtCertBasecertService;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 基础认证Service业务层处理
 *
 * @author licong
 * @date 2025-07-29
 */
@Service
@Slf4j
public class RtCertBasecertServiceImpl implements RtCertBasecertService
{
    @Autowired
    private RtCertBasecertMapper rtCertBasecertMapper;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;
    @Resource
    private RTLogDifferenceServiceImpl rtLogDifferenceService;
    /**
     * 查询基础认证
     *
     * @param id 基础认证主键
     * @return 基础认证
     */
    @Override
    public RtCertBaseCertDO selectRtCertBasecertById(String id)
    {
        RtCertBaseCertDO ret=rtCertBasecertMapper.selectRtCertBasecertById(id);
        return ret;
    }

    /**
     * 查询基础认证
     *
     * @param name 基础认证名称
     * @return 基础认证
     */
    @Override
    public RtCertBaseCertDO selectRtCertBasecertByName(String name){
        RtCertBaseCertDO ret=rtCertBasecertMapper.selectRtCertBasecertByName(name);
        return ret;
    }


    /**
     * 查询基础认证列表
     *
     * @param rtCertBasecert 基础认证
     * @return 基础认证
     */
    @Override
    public List<RtCertBaseCertDO> selectRtCertBasecertList(RtCertBaseCertDO rtCertBasecert)
    {
        return rtCertBasecertMapper.selectRtCertBasecertList(rtCertBasecert);
    }

    /**
     * 新增基础认证
     *
     * @param rtCertBasecert 基础认证
     * @return 结果
     */
    @Override
    public int insertRtCertBasecert(RtCertBaseCertDO rtCertBasecert)
    {
        String name=rtCertBasecert.getCertname();
        RtCertBaseCertDO old=rtCertBasecertMapper.selectRtCertBasecertByName(name);
        if(old!=null){
            throw RTErrorCodeEnum.CERT_NAME_EXISTS.getException("该基础认证已存在,请勿重复添加");
        }else{
            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                //return ResponseResult.fail("401", "用户未登录");
                throw new RuntimeException("用户未登录");
            }

            String snowId=  String.valueOf(snowflakeIdComponent.getInstance().nextId());
            Timestamp timestamp = new Timestamp(new Date().getTime());

            rtCertBasecert.setId(snowId);
            rtCertBasecert.setCreatetime(timestamp);
            rtCertBasecert.setCreateuserid(String.valueOf(user.getOid()));
            rtCertBasecert.setCreateusername(user.getName());

//            log.info("单元认证-新增{},{},{},{}",
//                     rtCertBasecert.getCertname(),
//                     snowId,
//                     timestamp,
//                     user.getName());
            rtLogDifferenceService.newLog(
                    rtCertBasecert,null,
                    rtCertBasecert.getId(),
                    rtCertBasecert.getCreateusername(),
                    "表单",
                    "修改",
                    ""
            );

            return rtCertBasecertMapper.insertRtCertBasecert(rtCertBasecert);
        }



    }

    /**
     * 修改基础认证
     *
     * @param rtCertBasecert 基础认证
     * @return 结果
     */
    @Override
    public int updateRtCertBasecert(RtCertBaseCertDO rtCertBasecert)
    {
        RtCertBaseCertDO old=rtCertBasecertMapper.selectRtCertBasecertById(rtCertBasecert.getId());
        if(old==null){
            throw new MessageException("该基础认证不存在,请检查");
        }else{
            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                //return ResponseResult.fail("401", "用户未登录");
                throw new RuntimeException("用户未登录");
            }

            Timestamp timestamp = new Timestamp(new Date().getTime());
            rtCertBasecert.setUpdatetime(timestamp);
            rtCertBasecert.setUpdateuserid(String.valueOf(user.getOid()));
            rtCertBasecert.setUpdateusername(user.getName());

            int r=rtCertBasecertMapper.updateRtCertBasecert(rtCertBasecert);
            if(r>0){
                rtLogDifferenceService.newLog(
                        rtCertBasecert,old,
                        rtCertBasecert.getId(),
                        rtCertBasecert.getCreateusername(),
                        "表单",
                        "修改",
                        ""
                );
            }
            return r;
        }
    }

    /**
     * 批量删除基础认证
     *
     * @param ids 需要删除的基础认证主键
     * @return 结果
     */
    @Override
    public int deleteRtCertBasecertByIds(List<String> ids)
    {
        return rtCertBasecertMapper.deleteRtCertBasecertByIds(ids);
    }

    /**
     * 删除基础认证信息
     *
     * @param id 基础认证主键
     * @return 结果
     */
    @Override
    public int deleteRtCertBasecertById(String id)
    {
        return rtCertBasecertMapper.deleteRtCertBasecertById(id);
    }
}

